<?php

namespace app\common\library;

class Str
{
    public static function generate_str($length,$type) {
        // 密码字符集，可任意添加你需要的字符
        if($type==1) {
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@-_';
        } elseif($type==2) {
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        } elseif($type==3) {
            $chars = 'ABCDEFGHKMNPQRSTUVWXYZ23456789';
        } else {
            $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        }
        $password ='';
        for ( $i = 0; $i < $length; $i++ ) {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符；
            // 第二种是取字符数组 $chars 的任意元素
            // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
            $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $password;
    }

    /**
     * @param $type     //返回格式：0大小写混合  1全大写  2全小写
     * @param $func     //sha512(返回128位) sha384(返回96位) sha256(返回64位) md5(返回32位)
     * @return string
     */
    public static function createSecret($type=0, $func='md5')
    {
        $uid = md5(uniqid(rand(), true) . microtime());
        $hash = hash($func, $uid);
        $arr = str_split($hash);
        foreach ($arr as $v) {
            if ($type == 0) {
                $newArr[] = empty(rand(0, 1)) ? strtoupper($v) : $v;
            }
            if ($type == 1) {
                $newArr[] = strtoupper($v);
            }
            if ($type == 2) {
                $newArr[] = $v;
            }
        }
        return implode('', $newArr);
    }

    public static function generateRandomString($length) {
        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        return substr(str_shuffle($characters), 0, $length);
    }

    public static function isPhoneNumber($str)
    {
        if (empty($str)) {
            return false;
        }
        $pattern = '/^1[3456789]\d{9}$/';
        return preg_match($pattern, $str);
    }

    /**
     * Notes:手机号脱敏
     * @param $mobile
     */
    public static function secedeSensitive($mobile)
    {
        return preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $mobile);// 输出：138****5678
    }

    /**
     * Notes:名字脱敏
     * @param $name
     * @return string
     */
    public static function processName($name)
    {
        if (mb_strlen($name, 'UTF-8') > 2) {
            return preg_replace("/(?<=\w)\w(?=\w)/u", "*", $name);
        }
        return mb_substr($name, 0, 1, 'UTF-8') . '*';
    }

    public static function similarity($str1, $str2) {
        $len1 = strlen($str1);
        $len2 = strlen($str2);
        $distance = levenshtein($str1, $str2);
        return 1 - ($distance / max($len1, $len2));
    }

    public static function extractStrings($pattern, $inputString) {
        // 使用 preg_match 函数找到第一个匹配的字符串
        preg_match($pattern, $inputString, $matches);

        // 返回第一个匹配的结果
        return $matches[0] ?? null;
    }

    public static function uuid()
    {
        //生成uuid
        return strtoupper(md5(uniqid(mt_rand(), true)));
    }

    public static function underscoreToCamelCase($string) {
        return preg_replace_callback('/_([a-zA-Z])/', function ($matches) {
            return strtoupper($matches[1]);
        }, $string);
    }
}